require 'rspec'
require 'webmock/rspec'

$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
require 'gamifier'

# WebMock.allow_net_connect!

RSpec.configure do |config|

  # config.before :all do
  #   Gamifier.set :uri, ENV.fetch("BADGEVILLE_URI") { "http://sandbox.v2.badgeville.com/api/berlin/" }
  #   Gamifier.set :key, ENV.fetch("BADGEVILLE_KEY") { "1234" }
  #   ENGINE = Gamifier.engine if !defined?(ENGINE)
  # end

  def player_attributes
    { :email => "user@foo.com", :site => "foo.com", :display_name => "Foo Bar" }
  end

  def user_attributes
    { :email => "user@foo.com" }
  end

  def find_or_create_test_user
    user = Gamifier.engine.users.find(user_attributes[:email]) || Gamifier.engine.users.build(user_attributes).save
    user.should_not be_nil
  end

  def destroy_test_player!
    player = Gamifier.engine.players.find_by_site_and_email(player_attributes[:site], player_attributes[:email])
    if player
      puts "Found player #{player}. Deleting..."
      player.destroy.should be_true
    end
  end

  def find_or_create_test_player
    find_or_create_test_user
    player = Gamifier.engine.players.find_by_site_and_email(player_attributes[:site], player_attributes[:email])
    if player.nil?
      player = Gamifier.engine.players.build(player_attributes).save
      player.should_not be_false
      player
    else
      player
    end
  end

end
